package com.avic.rong.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.avic.rong.R;
import com.avic.rong.bean.HomeBean;
import com.example.minato.minastore.utils.GlideApp;

import java.util.ArrayList;
import java.util.List;


public class PicAdapter extends PagerAdapter {

    private Context mActivity;
    private List<HomeBean.AdvertisementBean> jParamsList = new ArrayList<>();

    public void setjParamsList(List<HomeBean.AdvertisementBean> jParamsList) {
        this.jParamsList = jParamsList;
//        notifyDataSetChanged();
    }


    public PicAdapter(Context context, List<HomeBean.AdvertisementBean> tempList) {
        mActivity = context;
        this.jParamsList.clear();
        this.jParamsList = tempList;
    }


    @Override
    public int getCount() {
        return jParamsList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        view.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = LayoutInflater.from(mActivity.getApplicationContext()).inflate(R.layout.recommend_page_item, container, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
        if (jParamsList.size() > 0 && jParamsList.get(position).getContent() != null) {
            GlideApp.with(mActivity).load(jParamsList.get(position).getContent()).into(imageView);
            view.setOnClickListener(new BannerOnClickListener(mActivity, jParamsList.get(position).getUrl()));
        }
        container.addView(view);
        return view;
    }

    class BannerOnClickListener implements View.OnClickListener {

        Context context;
        String mUrl;

        public BannerOnClickListener(Context context, String skipUrl) {
            this.context = context;
            this.mUrl = skipUrl;
        }

        @Override
        public void onClick(View view) {

            //点击首页banner
            StringBuilder stringBuilder = new StringBuilder(mUrl);
            if (mUrl.contains("cn")){
                stringBuilder.insert(mUrl.indexOf("cn") + 2,"/app");
            }else{
                stringBuilder.insert(mUrl.indexOf("com") + 3,"/app");
            }
            if (this.mUrl.contains("/good/") || this.mUrl.contains("goods/goodsInfo")){
                if (!mUrl.contains("/app")){
                    if (this.mUrl.contains("goods/goodsInfo")){
                        mUrl = mUrl.replace("goods/goodsInfo","app/good/info");
                    }else if(this.mUrl.contains("good/info")){
                        mUrl = mUrl.replace("good/info","app/good/info");
                    }
                }

                if (!mUrl.endsWith("html")){
                    mUrl = mUrl + ".html";
                }
//                context.startActivity(CriterionDetailActivity.queryIntent(context,mUrl));
            }else if(mUrl.contains("/criterionList")){//标准列表http://m.rongrong.cn/biaozhun/criterionList
                mUrl = mUrl.replace("rongrong.cn","rongrong.cn/app");
//                context.startActivity(CriterionListActivity.queryIntent(context,mUrl));
            }else{
                mUrl = mUrl.replace("rongrong.cn","rongrong.cn/app");
//                context.startActivity(CriterionDetailActivity.queryIntent(context,mUrl));
            }
        }
    }


}
